package com.dc.system.controller;

import com.dc.common.aop.LogAop;
import com.dc.common.aop.LoginAop;
import com.dc.common.aop.ResubmitAop;
import com.dc.common.constants.ConstantsCommon;
import com.dc.common.controller.BaseController;
import com.dc.common.em.EnumOperationModule;
import com.dc.common.model.BaseModel;
import com.dc.system.model.SysRole;
import com.dc.system.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/system/role")
@Api(description = "角色相关接口")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    @LoginAop
    @ResubmitAop
    @LogAop(module = EnumOperationModule.No11102, type = ConstantsCommon.FUN_ADD )
    @ApiOperation(value = "新增角色&权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "角色名称", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "note", value = "角色备注", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "permissionStr", value = "总系统权限字符串(1,2,3,101)", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "subPermissionStr", value = "子系统权限字符串（11101:11111,11102:11111）", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "token", value = "防止表单重复提交的令牌", dataType = "string", paramType = "query")
    })
    @PostMapping("/insertRole")
    public BaseModel insertRole(BaseModel baseModel, SysRole role, String permissionStr, String subPermissionStr) throws Exception {
        return roleService.insertRole(baseModel, role, permissionStr, subPermissionStr, request);
    }

    @LoginAop
    @ApiOperation(value = "查询权限设置页总菜单内容（2类）")
    @PostMapping("/getPermissionMenus")
    public BaseModel getPermissionMenus(BaseModel baseModel) throws Exception {
        return roleService.getPermissionMenus(baseModel);
    }

    @LoginAop
    @ApiOperation(value = "查询当前登录用户总系统的菜单列表")
    @PostMapping("/getUserMenus")
    public BaseModel getUserMenus(BaseModel baseModel) throws Exception {
        return roleService.getUserMenus(baseModel, request);
    }

    @LoginAop
    @ApiOperation(value = "根据总系统模块的code值去查询当前用户子系统的权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "codeNum", value = "点击的总系统菜单的code值", required = true, dataType = "string", paramType = "query")
    })
    @PostMapping("/getUserSubMenusByCodeNum")
    public BaseModel getUserSubMenusByCodeNum(BaseModel baseModel, String codeNum) throws Exception {
        return roleService.getUserSubMenusByCodeNum(baseModel, request, codeNum);
    }

    @LoginAop
    @ApiOperation(value = "查询角色id和名字下拉列表")
    @PostMapping("/getRoleIdAndNames")
    public BaseModel getRoleIdAndNames(BaseModel baseModel) throws Exception {
        return roleService.getRoleIdAndNames(baseModel);
    }

    @LoginAop
    @LogAop(module = EnumOperationModule.No11102, type = ConstantsCommon.FUN_SEARCH )
    @ApiOperation(value = "列表/高级查询角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "rows", value = "行数", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "key", value = "关键字", dataType = "string", paramType = "query")
   })
    @PostMapping("/getRoles")
    public BaseModel<List<SysRole>> getRoles(BaseModel baseModel, SysRole role) throws Exception {
        return roleService.getRoles(baseModel, role,request);
    }

    @LoginAop
    @LogAop(module = EnumOperationModule.No11102, type = ConstantsCommon.FUN_UPDATE )
    @ApiOperation(value = "修改角色&权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色id", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "角色名称", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "note", value = "角色备注", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "permissionStr", value = "总系统权限字符串(1,2,3,101)", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "subPermissionStr", value = "子系统权限字符串（11101:11111,11102:11111）", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "version", value = "版本号", required = true, dataType = "Integer", paramType = "query")
    })
    @PostMapping("/updateRole")
    public BaseModel updateRole(BaseModel baseModel, SysRole role, String permissionStr, String subPermissionStr) throws Exception {
        return roleService.updateRole(baseModel, role, permissionStr, subPermissionStr, request);
    }

    @LoginAop
    @ApiOperation(value = "根据角色id查询权限code值")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色id", required = true, dataType = "Integer", paramType = "query")
    })
    @PostMapping("/getPermissionByRoleId")
    public BaseModel getPermissionByRoleId(BaseModel baseModel, SysRole role) throws Exception {
        return roleService.getPermissionByRoleId(baseModel,role);
    }

    @LoginAop
    @LogAop(module = EnumOperationModule.No11102, type = ConstantsCommon.FUN_DELETE )
    @ApiOperation(value = "删除角色&权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色id", required = true, dataType = "string", paramType = "query")
    })
    @PostMapping("/delRole")
    public BaseModel delRole(BaseModel baseModel, SysRole role) throws Exception {
        return roleService.delRole(baseModel, role, request);
    }


}
